var Lib = {}
const baseurl="https://caocan.beiyii.xyz/"
const resurl="http://res.caocan.beiyii.xyz/"
const resurl_ssl="https://res.caocan.beiyii.xyz/"
Lib.install = function (Vue, options) {
    Vue.prototype.$post = function (api,data,cb,error_cb) {
        let message=""
        let apilist=api.split("|")
        if(apilist.length>1){
            api=apilist[0]
            message=apilist[1]
        }
        let _t=this
        if (data == null) {
            data = {}
        }
        var token = wx.getStorageSync("token")
        if (token != null) {
            data.token = token
        }
        var url = baseurl + api
        if(message!=""){
            _t.showloading(message)
        }
        wx.request({
            url: url,
            data: data,
            method: 'POST',
            success: function(res) {
                if(res.statusCode!=200){
                    _t.toast("接口状态不正确:"+api)
                    return
                }

                let data=res.data
                if(data.error_code==1){
                    if (error_cb) {
                        error_cb(res.data)
                    }else {
                        _t.toast(data.message)
                    }
                    return
                }
                if(data.error_code==300){
                    if(error_cb==null){
                        _t.check()
                    }else{
                        error_cb()
                    }
                    return
                }
                if(data.error_code==0){
                    cb(data.result)
                }
            },
            fail: function(res) {
                app.toast("接口请求失败" + _url)
            },
            complete:function (res) {
                if(message!=""){
                    _t.hideloading()
                }
            }

        })
    }
    Vue.prototype.toast=function(message){
        wx.showToast({
            title: message,
            icon: 'none',
            duration: 2000
        })
    }
    Vue.prototype.check=function() {
        //this.isready=true
        let _t=this
        _t.$post("api/site/init", null, function(data) {
            _t.site=data
            _t.init()
        }, function() {
            _t.login()
        })
    }
    Vue.prototype.login=function () {
        let _t=this
        wx.login({
            success: function (lres) {
                if (lres.code) {
                    _t.$post("api/user/login",{code:lres.code},function (token) {
                        wx.setStorageSync("token",token)
                        _t.check()
                    })
                } else {
                    _that.toast("登录失败，请重新打开小程序")
                }
            }
        });
    }

    Vue.prototype.datainit=function (data) {
        data.site={avatar:null,nickname:null,poster:null}
        data.baseurl=baseurl
        data.resurl=resurl
        data.resurl_ssl=resurl_ssl
        data.isready=false
        return data
    }
    Vue.prototype.get=function (key) {
        return this.$root.$mp.query[key]
    }
    Vue.prototype.gettime=function () {
        return parseInt(Date.parse(new Date())/1000);
    }
    Vue.prototype.sectostr=function (audio_mid_time) {
        audio_mid_time=parseInt(audio_mid_time)
        let sec=audio_mid_time%60
        let min=parseInt(audio_mid_time/60)
        if(sec<10){
            sec="0"+sec
        }
        if(min<10){
            min="0"+min
        }
        return min+":"+sec
    }
    Vue.prototype.showloading=function (message) {
        wx.showLoading({
            title:message,
        })

    }
    Vue.prototype.hideloading=function (message) {
        wx.hideLoading()
    }
    Vue.prototype.cache = function (name,val) {
        if (val == null) {
            var v = wx.getStorageSync(name);
            v = v == "" ? null : v;
            return v;
        }
        if (val == "del") {
            wx.removeStorageSync(name)
            return;
        }
        wx.setStorageSync(name, val)
    }


}
module.exports = Lib;